/*
 * Copyright (C) 2014 Eddie
 */
package edu.arhs.team1100.worldclass.forms;

import edu.arhs.team1100.worldclass.objects.Match;
import edu.arhs.team1100.worldclass.handlers.MatchHandler;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Eddie
 */
public class ScoringFrame extends javax.swing.JFrame {

    private ScoringFrame classObject = this;
    private int currentlySelectedRow;
    private MatchPanel[] panels;

    /**
     * Creates new form ScoringFrame
     */
    public ScoringFrame() {
        initComponents();
        loadMatches();
        setupClick();
        currentlySelectedRow = -1;
        panels = new MatchPanel[MatchHandler.getMatches().size()];

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        matchTable = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        matchTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Match Number", "Team Number", "null"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        matchTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        matchTable.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(matchTable);
        if (matchTable.getColumnModel().getColumnCount() > 0) {
            matchTable.getColumnModel().getColumn(0).setResizable(false);
            matchTable.getColumnModel().getColumn(1).setResizable(false);
            matchTable.getColumnModel().getColumn(2).setMinWidth(0);
            matchTable.getColumnModel().getColumn(2).setPreferredWidth(0);
            matchTable.getColumnModel().getColumn(2).setMaxWidth(0);
        }

        jPanel1.setPreferredSize(new java.awt.Dimension(416, 515));
        jPanel1.setLayout(new java.awt.CardLayout());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1))
                .addGap(14, 14, 14))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable matchTable;
    // End of variables declaration//GEN-END:variables

    private void loadMatches() {
        DefaultTableModel model = (DefaultTableModel) matchTable.getModel();
        List<Match> matches = MatchHandler.getMatches();
        Collections.sort(matches, new Comparator() {
            public int compare(Object o1, Object o2) {
                Match m1 = (Match) o1;
                Match m2 = (Match) o2;
                if (m1.getMatchNumber() != m2.getMatchNumber()) {
                    return m1.getMatchNumber() - m2.getMatchNumber();
                } else {
                    return m1.getTeamNumber() - m2.getTeamNumber();
                }
            }
        });

        for (Match match : matches) {
            model.addRow(new Object[]{match.getMatchNumber(), match.getTeamNumber(), match});
        }
    }

    private void setupClick() {
        matchTable.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent me) {
                if (matchTable.getSelectedRow() >= 0 && matchTable.getSelectedRow() != currentlySelectedRow) {
                    currentlySelectedRow = matchTable.getSelectedRow();
                    MatchPanel jPanel2;
                    if (panels[currentlySelectedRow] != null) {
                        jPanel2 = panels[currentlySelectedRow];
                    } else {
                        jPanel2 = new MatchPanel((Match) matchTable.getValueAt(currentlySelectedRow, 2));
                        panels[currentlySelectedRow] = jPanel2;
                    }

                    jPanel1.removeAll();
                    jPanel1.add(jPanel2);
                    jPanel2.repaint();
                    jPanel1.revalidate();
                }

            }
        });
    }

}
